package com.scuvc.controller.admin;

import com.scuvc.entity.CarInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.JMenuBarUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;

public class UpdateCarView extends JFrame {
    public void updateStart(){
        setTitle("修改汽车");
        //设置窗体起始位置
        setLocation(400,100);
        //设置窗体大小
        setSize(570,570);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_addCar.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f, getWidth(),getHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(getWidth(),getHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());
        //设置标题栏
        setTop(pan);

        JPanel panCenter = new JPanel();
        panCenter.setOpaque(false);
        GridLayout grid = new GridLayout(7,4);
        grid.setVgap(20);
        grid.setHgap(10);
        panCenter.setLayout(grid);
        Font font = new Font("仿宋",Font.BOLD,24);

        JLabel j1L1 = new JLabel();
        JLabel j1L2 = new JLabel("车\u0020牌\u0020号：");
        j1L2.setFont(font);
        j1L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j1f3 = new JTextField();
        j1f3.setFont(font);
        JLabel j1L4 = new JLabel();

        JLabel j2L1 = new JLabel();
        JLabel j2L2 = new JLabel("租\u3000\u3000金：");
        j2L2.setFont(font);
        j2L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j2f3 = new JTextField();
        j2f3.setFont(font);
        JLabel j2L4 = new JLabel();

        JLabel j3L1 = new JLabel();
        JLabel j3L2 = new JLabel("市\u0020场\u0020价：");
        j3L2.setFont(font);
        j3L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j3f3 = new JTextField();
        j3f3.setFont(font);
        JLabel j3L4 = new JLabel();

        JLabel j4L1 = new JLabel();
        JLabel j4L2 = new JLabel("押\u3000\u3000金：");
        j4L2.setFont(font);
        j4L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j4f3 = new JTextField();
        j4f3.setFont(font);
        JLabel j4L4 = new JLabel();

        JLabel j5L1 = new JLabel();
        JLabel j5L2 = new JLabel("租恁情况：");
        j5L2.setFont(font);
        j5L2.setHorizontalAlignment(SwingConstants.CENTER);
        String[] rent = {"可租","已租"};
        JComboBox<String> rentJcb = new JComboBox<>(rent);
        rentJcb.setFont(font);
        JLabel j5L4 = new JLabel();

        JLabel j6L1 = new JLabel();
        JLabel j6L2 = new JLabel("上/下\u0020架：");
        j6L2.setFont(font);
        j6L2.setHorizontalAlignment(SwingConstants.CENTER);
        String[] upDown = {"上架","下架"};
        JComboBox<String> upJcb = new JComboBox<>(upDown);
        upJcb.setFont(font);
        JTextField j6f3 = new JTextField();
        j6f3.setFont(font);
        JLabel j6L4 = new JLabel();

        JLabel j7L1 = new JLabel();
        JButton j7b2 = new JButton("修\u3000改");
        j7b2.setFont(font);
        JButton j7b3 = new JButton("重\u3000置");
        j7b3.setFont(font);
        JLabel j7L4 = new JLabel();
        panCenter.add(j1L1);
        panCenter.add(j1L2);
        panCenter.add(j1f3);
        panCenter.add(j1L4);

        panCenter.add(j2L1);
        panCenter.add(j2L2);
        panCenter.add(j2f3);
        panCenter.add(j2L4);

        panCenter.add(j3L1);
        panCenter.add(j3L2);
        panCenter.add(j3f3);
        panCenter.add(j3L4);

        panCenter.add(j4L1);
        panCenter.add(j4L2);
        panCenter.add(j4f3);
        panCenter.add(j4L4);

        panCenter.add(j5L1);
        panCenter.add(j5L2);
        panCenter.add(rentJcb);
        panCenter.add(j5L4);

        panCenter.add(j6L1);
        panCenter.add(j6L2);
        panCenter.add(upJcb);
        panCenter.add(j6L4);

        panCenter.add(j7L1);
        panCenter.add(j7b2);
        panCenter.add(j7b3);
        panCenter.add(j7L4);

        pan.add(panCenter,BorderLayout.CENTER);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //点击事件
        j7b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取车牌号
                String carNo = j1f3.getText();
                //通过车牌号查询汽车信息
                CarInfo car = StaticUtils.carInfoService.getCarInfoByCarNoOne(carNo);
                Long price = car.getPrice() ;
                String prices = j2f3.getText();
                if(prices!=null && !prices.equals("")){
                    price = Long.valueOf(prices);
                }
                Double car_amount = car.getCar_amount() ;
                String car_amounts = j3f3.getText();
                if(car_amounts!=null && !car_amounts.equals("")){
                    car_amount = Double.valueOf(car_amounts);
                }
                Long deposit = car.getDeposit() ;
                String deposits = j4f3.getText();
                if(deposits!=null && !deposits.equals("")){
                    deposit = Long.valueOf(deposits);
                }
                String statu = (String)rentJcb.getSelectedItem();
                Integer status = 0;
                if(statu.equals("可租")){
                    status = 1 ;
                }
                String usables = (String)upJcb.getSelectedItem();
                Integer usable = 0 ;
                if(usables.equals("上架")){
                    usable = 1 ;
                }
                // TODO 调用updateCarInfo方法实现修改业务

                if(row>=1){
                    AdminView av = new AdminView();
                    StaticUtils.pageBean.setCurPage(1);
                    StaticUtils.pageBean.setPrePage();
                    StaticUtils.pageBean.setNextPage();
                    StaticUtils.pageBean.setDataList(StaticUtils.carInfoService.getAll());
                    StaticUtils.pageBean.setTotalPage();
                    List<CarInfo> list = StaticUtils.carInfoService.getAllPage(
                                    StaticUtils.pageBean.getCurPage(), StaticUtils.pageBean.getPageSize());
                    //创建存储JLabel对象的集合
                    List<JLabel> carList = new ArrayList<>();
                    //将跟新数据封装到JLabel对象中，并将JLabel添加到面板中
                    JMenuBarUtils.setData(carList,list,panCenter,"admin");
                    //记录当前最新数据
                    StaticUtils.refresh = carList;
                    setVisible(false);
                    av.adminShow();
                }else{
                    UpdateCarView acv = new UpdateCarView();
                    acv.updateStart();
                }
            }
        });
        j7b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                j1f3.setText("");
                j2f3.setText("");
                j3f3.setText("");
                j4f3.setText("");
            }
        });
        FocusListener focusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j1f3.setBackground(Color.BLACK);
                j1f3.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
               j1f3.setBackground(Color.WHITE);
               j1f3.setForeground(Color.BLACK);
               String text = j1f3.getText();
               if(text.equals("")){
                   JOptionPane.showMessageDialog(null,
                           "车牌号不能为空","提示",
                           JOptionPane.INFORMATION_MESSAGE);
               }
            }
        };
        j1f3.addFocusListener(focusListener);
        StaticUtils.setFoucusListener(j1f3);
        StaticUtils.setFoucusListener(j2f3);
        StaticUtils.setFoucusListener(j3f3);
        StaticUtils.setFoucusListener(j4f3);
    }


    private void setTop(JPanel pan) {
        JPanel panTop = new JPanel();
        panTop.setLayout(new GridLayout(1,1));
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("车辆入库");
        jTop.setPreferredSize(new Dimension(
                jTop.getPreferredSize().width,100));
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);
        panTop.add(jTop);
        pan.add(panTop,BorderLayout.NORTH);
    }
}
